package com.example.rule.expression;

import com.example.rule.db.entity.PymtRule;
import com.example.rule.db.entity.PymtRuleOperator;
import com.example.rule.core.Representation;
import com.example.rule.util.CacheUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author Jason - Chusifeng
 * @version 1.0.0
 * @className WhereTimeTimesExpression.java
 * @packageName com.example.rule.expression
 * @createTime 2022年10月12日 09:26:44
 * @description 创建指定位置（时间，出现次数）的表达式
 */
public class WhereTimeTimesRepresentation implements Representation {

    private static final Pattern OPERATORS = Pattern.compile("[><=]+");
    private static final String EXPRESSION_FIELD_1 = "addressName";

    private static final String EXPRESSION_FIELD_2 = "times";

    private static final String EXPRESSION_FIELD_3 = "time before";
    private static final String EXPRESSION_FIELD_4 = "time after";
    private static final String EXPRESSION_FIELD_5 = "time is";

    public String[] createExpressions(PymtRule rule) {
        List<PymtRuleOperator> operatorList = CacheUtil.getRuleOperators();
        StringBuilder sb = new StringBuilder();
        String operator = getOpe(rule.getOperator1(), operatorList);
        sb.append(getExpression(operator, rule.getValue1()));
        sb.append(",");
        //operator2
        if (StringUtils.isNotBlank(rule.getOperator2())) {
            operator = getOpe(rule.getOperator2(), operatorList);
            sb.append(getExpression(operator, rule.getValue2()));
        }
        sb.append(",");
        //operator3
        if (StringUtils.isNotBlank(rule.getOperator3())) {
            operator = getOpe(rule.getOperator3(), operatorList);
            sb.append(getExpression(operator, rule.getValue3()));
        }
        return sb.toString().split(",");
    }

    private static String getExpression(String operator, String value) {
        StringBuilder sb = new StringBuilder();
        List<String> express = getOperatorEx(operator, value);
        if (StringUtils.isBlank(express.get(0))) {
            sb.append(express.get(1));
        } else {
            sb.append(express.get(0));
            sb.append(express.get(1));
            sb.append(value);
            if (operator.contains("location")) {
                sb.append("'");
            }
        }
        return sb.toString();
    }

    private static String getOpe(String name, List<PymtRuleOperator> operatorList) {
        PymtRuleOperator ruleOperator = operatorList.stream().filter(op -> op.getPkPymtRuleOperator().equals(Integer.valueOf(name))).findFirst().orElseThrow(() -> new RuntimeException("don't get operator"));
        return ruleOperator.getValue();
    }

    private static List<String> getOperatorEx(String operator, String value) {
        String field = "";
        String op = "";
        if (operator.contains("location")) {
            field = EXPRESSION_FIELD_1;
            op = " == '";
        }
        if (operator.contains(EXPRESSION_FIELD_3)) {
            op = "T(com.example.rule.core.Condition).isBefore(#root.time,'" + value + "')";
        }
        if (operator.contains(EXPRESSION_FIELD_4)) {
            op = "T(com.example.rule.core.Condition).isAfter(#root.time,'" + value + "')";
        }
        if (operator.contains(EXPRESSION_FIELD_5)) {
            op = "T(com.example.rule.core.Condition).isEqual(#root.time,'" + value + "')";
        }
        if (OPERATORS.matcher(operator).matches()) {
            field = EXPRESSION_FIELD_2;
            op = operator;
        }
        return Arrays.asList(field, op);
    }
}
